package com.auto.survey.activity.monitoring.adapter

import android.text.TextUtils
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.recyclerview.widget.RecyclerView
import com.auto.survey.R
import com.auto.survey.activity.monitoring.entity.Point

class PointManagerAdapter : RecyclerView.Adapter<PointManagerAdapter.CustomViewHodler>() {
    private var mItems :List<Point> = mutableListOf()
    private var mItemClickListener: OnPointClickItemListener?= null
    private var mUpdateClickListener: OnPointUpdateClickListener?=null
    private var mDeleteClickListener: OnPointDeleteClickListener?= null
    fun setData(items:List<Point>) {
        mItems = items
        notifyDataSetChanged()
    }

    fun setOnItemClickListener (onItemClickListener: OnPointClickItemListener) {
        mItemClickListener = onItemClickListener
    }

    fun setOnDeleteClickListener(listener: OnPointDeleteClickListener) {
        mDeleteClickListener = listener
    }

    fun setOnUpdateClickListener(listener: OnPointUpdateClickListener) {
        mUpdateClickListener = listener
    }


    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CustomViewHodler {
        val view = LayoutInflater.from(parent.context).inflate(R.layout.adapter_point_manager,parent, false)
        return CustomViewHodler(view)
    }

    override fun onBindViewHolder(holder: CustomViewHodler, position: Int) {
        val pName = mItems[position].pointName
        val pType = mItems[position].pointType
        if (TextUtils.isEmpty(pType)) {
            holder.tv_name.text= pName
        } else{
            val content = "$pName($pType)"
            holder.tv_name.text = content
        }

        if (mItems[position].pointX == 0.0) {
            holder.tv_x.text = ""
        } else {
            holder.tv_x.text=mItems[position].pointX.toString()
        }

        //y
        if (mItems[position].pointY == 0.0) {
            holder.tv_y.text = ""
        } else {
            holder.tv_y.text=mItems[position].pointY.toString()
        }

        //z
        if (mItems[position].pointZ == 0.0) {
            holder.tv_z.text = ""
        } else {
            holder.tv_z.text=mItems[position].pointZ.toString()
        }

        holder.root.setOnClickListener {
            mItemClickListener?.ontItemPointClick(mItems[position], position)
        }
    }

    override fun getItemCount(): Int {
        if (mItems.isNotEmpty()) {
            return mItems.size
        }
        return 0
    }


    inner class CustomViewHodler(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val root = itemView.findViewById<ConstraintLayout>(R.id.cl_pm_root)
        val tv_name = itemView.findViewById<TextView>(R.id.tv_pm_name)
        val delete = itemView.findViewById<TextView>(R.id.tv_pm_delete)
        val update = itemView.findViewById<TextView>(R.id.tv_pm_update)
        val tv_x = itemView.findViewById<TextView>(R.id.tv_pm_xn)
        val tv_y = itemView.findViewById<TextView>(R.id.tv_pm_ye)
        val tv_z = itemView.findViewById<TextView>(R.id.tv_pm_zh)
    }

    interface OnPointUpdateClickListener {
        fun onUpdateClick(point:Point, updatePosition:Int)
    }

    interface OnPointDeleteClickListener {
        fun onDelteClick(point:Point, deletePosition:Int)
    }


    interface OnPointClickItemListener {
        fun ontItemPointClick(point:Point, position:Int)
    }
}